package com.ltmonitor.web.controller.mobile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.Department;
import com.ltmonitor.entity.TenantEntity;
import com.ltmonitor.entity.UserInfo;
import com.ltmonitor.service.IDepartmentService;
import com.ltmonitor.service.IUserInfoService;
import com.ltmonitor.web.controller.GenericAction;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.vo.TreeNode;

@Controller
@RequestMapping("/mobile")
public class MobileDepartmentAction extends GenericAction {

	@Autowired
	private IUserInfoService userInfoService;

	@ResponseBody
	@RequestMapping("/viewDep.action")
	public JsonMessage view(@RequestParam(required = false, defaultValue="0") long depId) {
		try {
			Department dep = null;
			if (depId > 0) {
				 dep = this.departmentService.load(depId);
			}else
			{
				dep = new Department();		
			}
			return json(true, dep);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, "发生错误:"+ex.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("/depList.action")
	public JsonMessage depList() {
		try {
			if (this.getOnlineUser() == null)
				return json(false, "会话过期，请重新登录");

			String queryId = "statisticVehicleNum";
			Map params = this.getParams();

			params.put("userId", this.getOnlineUser().getEntityId());
			params.put("depIdList", super.getAuthorizedDepIdList());

			List rs = this.queryService.query(queryId, params);

			for (Object obj : rs) {
				Map data = (Map) obj;

				int depId = (Integer) data.get("depId");

				Department dep = this.departmentService.fetchDepartment(depId);
				if (dep.getParentId() > 0) {
					Department parentDep = this.departmentService
							.fetchDepartment(dep.getParentId());
					data.put("parentName", parentDep.getName());
				} else
					data.put("parentName", "");
				data.put("name", dep.getName());
				data.put("parentId", dep.getParentId());

			}

			return json(true, rs);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return json(false, e.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("/saveOrUpdateDep.action")
	public JsonMessage save(Department dep) {
		try {
			long depId = dep.getEntityId();
			if (depId > 0) {
				//避免无限递归
				if (depId == dep.getParentId()) {
					return json(false, "保存错误:上级车组不能是本车组!");
				}

				List<Long> childDepIdList = this.departmentService
						.getDepIdList(depId);
				if (childDepIdList.contains(dep.getParentId())) {
					return json(true, "上级车组不能同时是本车组的下级车组!");
				}
			}
			// this.save();
			departmentService.saveDepartment(dep);
			
			/**
			 * 如果用户拥有新增的部门的上级部门的权限，将自动拥有新增部门的权限
			 */
			if (depId == 0) {
				this.userInfoService.assignDepartmentToUser(dep, this.getOnlineUser().getEntityId());
			}
			UserInfo u = (UserInfo) this.userInfoService.load(this.getOnlineUser().getEntityId());
			super.setAuthorizedDep(u);

			return json(true, "保存成功");
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}

	}

	@ResponseBody
	@RequestMapping("/deleteDep.action")
	public JsonMessage fakeDelete(long depId) {
		try {
			String hql = "from VehicleData where depId = ? and deleted = ?";
			Object obj = this.vehicleDataService.find(hql,
					new Object[] { depId, false });
			if (obj != null) {
				return super.json(false, "该车组下分配有车辆，不能删除!");
			}
			hql = "from Department where parentId = ? and deleted = ?";
			obj = this.departmentService.find(hql, new Object[] { depId, false });
			if (obj != null) {
				return super.json(false, "该车组下有子部门，不能删除!");
			}

			departmentService.deleteFakeById(depId);
			return super.json(true, "记录已被成功刪除!");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return json(false, e.getMessage());
		}
	}

	

}
